package com.goodmr.passport.webapp.Serivce.impl;

import com.goodmr.passport.account.PlatformModel;
import com.goodmr.passport.password.PasswordDao;
import com.goodmr.passport.webapp.Serivce.LoginService;
import com.goodmr.passport.webapp.enums.ErrorEnum;
import com.goodmr.passport.webapp.exception.BadRequestException;
import com.goodmr.passport.webapp.model.request.SigninPwdReq;
import com.goodmr.passport.webapp.model.request.SigninReq;
import com.goodmr.passport.webapp.model.response.SigninResp;
import lombok.NonNull;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.validation.constraints.NotNull;

/**
 * 帐号密码登录服务
 *
 * @author <a href="mailto:bingdong13@msn.com">bigo</a>
 */
@Service("loginPwdService")
public class LoginPwdServiceImpl extends AccountServiceImpl implements LoginService {

    @Autowired
    private PasswordDao passwordDao;

    /**
     * 帐号密码登录
     *
     * @param data 帐号信息
     * @return signin response
     */
    @Override
    public SigninResp login(@NonNull SigninReq data) {

        SigninPwdReq body = (SigninPwdReq) data;

        validateAccount(body.getAccount());

        validatePassword(body.getPassword());

        // 帐号信息
        PlatformModel platform = getByAccount(body.getAccount());

        // 密码正确性校验
        if (!passwordDao.verify(platform.getAccountId(), body.getPassword())) {
            throw new BadRequestException(ErrorEnum.ACCOUNT_PASSWORD_ERROR);
        }

        return doAfter(platform);
    }

    /**
     * 帐号登录成功，其它逻辑处理
     *
     * @param platform 帐号平台信息
     * @return signin response
     */
    private SigninResp doAfter(@NonNull PlatformModel platform) {

        // todo 添加设备信息

        // todo 生成登录日志

        // todo 生成授权凭证

        return new SigninResp("access_token", "refresh_token");
    }
}
